package com.inhim.pjhim.activity;

import android.content.Intent;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.inhim.pjhim.R;
import com.inhim.pjhim.app.BaseActivity;
import com.inhim.pjhim.entity.SMSResult;
import com.inhim.pjhim.http.MyOkHttpClient;
import com.inhim.pjhim.http.Urls;
import com.inhim.pjhim.view.BToast;
import com.inhim.pjhim.widget.CountryCodeDialog;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.Request;

public class RegisterActivity extends BaseActivity implements View.OnClickListener {

    private EditText ed_mobile,ed_password,ed_code;
    private TextView tv_geSMS,tv_agreement,tvCountryCode;
    private ImageView iv_close;
    private Button btn_login;
    private Gson gson;
    private CheckBox cb_save_pw;
    private boolean mIsOk;
    private CheckBox rb_close_eyes;
    private String countryCode = "+86";
    @Override
    public Object offerLayout() {
        return R.layout.activity_register_new;
    }

    @Override
    public void onBindView() {
        gson=new Gson();
        initView();
    }

    @Override
    public void destory() {

    }

    private void initView(){
        iv_close=findViewById(R.id.iv_close);
        tvCountryCode=findViewById(R.id.tvCountryCode);
        iv_close.setOnClickListener(this);
        ed_mobile=findViewById(R.id.ed_mobile);
        ed_password=findViewById(R.id.ed_password);
        btn_login=findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);
        tv_geSMS=findViewById(R.id.tv_geSMS);
        tv_geSMS.setOnClickListener(this);
        cb_save_pw=findViewById(R.id.cb_save_pw);
        cb_save_pw.setOnCheckedChangeListener((buttonView, isChecked) -> {
            mIsOk=isChecked;
        });
        tv_agreement = findViewById(R.id.tv_agreement);
        tv_agreement.setOnClickListener(this);
        rb_close_eyes=findViewById(R.id.rb_close_eyes);
        rb_close_eyes.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if(isChecked){
                ed_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }else{
                ed_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }
        });
        ed_code=findViewById(R.id.ed_code);
        tvCountryCode.setOnClickListener(v -> {
            showChooseCountyCodeDialog();
        });
    }

    private void showChooseCountyCodeDialog() {
        CountryCodeDialog dialog = new CountryCodeDialog(this,R.style.ActionSheetDialogStyle);
        dialog.setListenner(new CountryCodeDialog.Listenner() {
            @Override
            public void onCodeSelected(String code) {
                countryCode  = code;
                tvCountryCode.setText(code);
            }
        });
        dialog.show();
    }

    private void onRegister() {
        if (ed_mobile.getText().toString().equals("")) {
            BToast.showText("请输入手机号码 ", Toast.LENGTH_LONG, false);
            return;
        }
        if (ed_code.getText().toString().equals("")) {
            BToast.showText("请输入验证码 ", Toast.LENGTH_LONG, false);
            return;
        }
        if (ed_password.getText().toString().equals("")) {
            BToast.showText("请输入密码 ", Toast.LENGTH_LONG, false);
            return;
        }
        if (!mIsOk) {
            BToast.showText("请阅读并同意用户协议 ", Toast.LENGTH_LONG, false);
            return;
        }
        String examUrl = Urls.onRegister();
        FormBody.Builder formBody=new FormBody.Builder();
        formBody.add("mobile",ed_mobile.getText().toString());
        formBody.add("password",ed_password.getText().toString());
        //formBody.add("openId",ed_mobile.getText().toString());
        formBody.add("validate",ed_code.getText().toString());
        MyOkHttpClient.getInstance().asyncPost(examUrl, formBody.build(),new MyOkHttpClient.HttpCallBack() {
            @Override
            public void onError(Request request, IOException e) {

            }

            @Override
            public void onSuccess(Request request, String results) {
                SMSResult smsResult=gson.fromJson(results,SMSResult.class);
                if(smsResult.getMsg().equals("success")){
                    BToast.showText("注册成功 ", Toast.LENGTH_LONG, true);
                }

            }
        });
    }

    private void sendSMS() {
        if (ed_mobile.getText().toString().equals("")) {
            BToast.showText("请输入手机号码 ", Toast.LENGTH_LONG, false);
            return;
        }
        String internationalCode = countryCode.substring(1);
        String examUrl = Urls.sendSMS(ed_mobile.getText().toString(),internationalCode);
        MyOkHttpClient.getInstance().asyncGetNoToken(examUrl, new MyOkHttpClient.HttpCallBack() {
            @Override
            public void onError(Request request, IOException e) {
            }

            @Override
            public void onSuccess(Request request, String results) {
                SMSResult smsResult=gson.fromJson(results,SMSResult.class);
                if(smsResult.getMsg().equals("success")){
                    BToast.showText("验证码发送成功 ", Toast.LENGTH_LONG, true);
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_weixin:
                break;
            case R.id.btn_login:
                onRegister();
                break;
            case R.id.iv_close:
                finish();
                break;
            case R.id.tv_geSMS:
                sendSMS();
                break;
            case R.id.tv_agreement:
                Intent intent = new Intent(RegisterActivity.this, WebViewActivity.class);
                intent.putExtra("Type", "agreement");
                startActivity(intent);
                break;
            default:
                break;
        }
    }
}
